﻿//-----------------------------------------------------------------------
// <copyright file="IsolatedStorageLogger.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.Engine.Log
{
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Text;

    using ChatBox.DomainObjects;
    using ChatBox.Interfaces;

    /// <summary>
    /// log to a file under Isolated Storage
    /// </summary>
    public class IsolatedStorageLogger : ILogger
    {
        /// <summary>
        /// Write an exception to the log
        /// </summary>
        /// <param name="message">The exception message</param>
        /// <param name="ex">The exception to write</param>
        public void LogException(string message, Exception ex)
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly();

            using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(
                IsolatedStorageLogHelper.StorageFileName,
                FileMode.Append,
                FileAccess.Write,
                storage))
            {
                using (StreamWriter writer = new StreamWriter(fileStream))
                {
                    writer.WriteLine();
                    writer.WriteLine("-------------");
                    writer.WriteLine("Logged at " + DateTime.Now);
                    writer.WriteLine("By version " + VersionConstants.CurrentVersion);
                    writer.WriteLine("Message: " + message);
                    if (ex != null)
                    {
                        WriteExceptionToStream(ex, writer);
                    }

                    writer.Close();
                }
            }
        }

        /// <summary>
        /// Write the exception to a stream
        /// </summary>
        /// <param name="ex">the exception to write</param>
        /// <param name="writer">the stream to write it to</param>
        private static void WriteExceptionToStream(Exception ex, TextWriter writer)
        {
            StringBuilder stringBuilder = new StringBuilder();
            AppendException(ex, stringBuilder);
            writer.WriteLine(stringBuilder.ToString());
        }

        /// <summary>
        /// Append exception data to the string builder
        /// </summary>
        /// <param name="ex">the exception</param>
        /// <param name="stringBuilder">the stringbuilder</param>
        private static void AppendException(Exception ex, StringBuilder stringBuilder)
        {
            stringBuilder.AppendLine("Exception Type: " + ex.GetType().Name);
            stringBuilder.AppendLine("Exception Message: " + ex.Message);
            stringBuilder.AppendLine("Stack trace: " + ex.StackTrace);

            if (ex.InnerException != null)
            {
                stringBuilder.AppendLine("Inner Exception:");
                AppendException(ex.InnerException, stringBuilder);
            }
        }
    }
}